package com.qi.utils;

import com.qi.exception.ServerException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;

@Component
public class MailUtil {

    @jakarta.annotation.Resource
    private JavaMailSenderImpl mailSender;

    private String buildContentApproval(String name, String clubName) {
        Resource resource = new ClassPathResource("approval.ftl");
        InputStream in = null;
        BufferedReader fileReader = null;
        StringBuilder sb = new StringBuilder();
        String line = "";
        try {
            in = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(in));
            while ((line = fileReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            throw new ServerException("发送邮件读取模板失败。" + e.getMessage());
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return MessageFormat.format(sb.toString(), name, clubName, name, clubName, clubName);
    }

    private String buildContentReject(String name, String clubName) {
        Resource resource = new ClassPathResource("reject.ftl");
        InputStream in = null;
        BufferedReader fileReader = null;
        StringBuilder sb = new StringBuilder();
        String line = "";
        try {
            in = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(in));
            while ((line = fileReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            throw new ServerException("发送邮件读取模板失败。" + e.getMessage());
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return MessageFormat.format(sb.toString(), name, clubName, name, clubName, clubName);
    }

    public void sendEmailApproval(String to, String name, String clubName) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //邮箱发送内容组成
            helper.setSubject("申请加入社团结果反馈");
            helper.setText(buildContentApproval(name, clubName), true);
            helper.setTo(to);
            helper.setFrom("朱安琦" + '<' + "xlimou@163.com" + '>');
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            throw new ServerException("邮件发送失败。" + e.getMessage());
        }
    }

    public void sendEmailReject(String to, String name, String clubName) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //邮箱发送内容组成
            helper.setSubject("申请加入社团结果反馈");
            helper.setText(buildContentReject(name, clubName), true);
            helper.setTo(to);
            helper.setFrom("朱安琦" + '<' + "xlimou@163.com" + '>');
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            throw new ServerException("邮件发送失败。" + e.getMessage());
        }
    }
}
